#!/usr/bin/env python
# -*- coding: utf-8 -*-

"""
Author:Lijiacai
Email:1050518702@qq.com
===========================================
CopyRight@JackLee.com
===========================================
"""
import datetime
import hashlib
import os
import sys
import logging
import json
import time
import requests
import schedule

cur_dir = os.path.split(os.path.realpath(__file__))[0]
sys.path.append("%s/.." % cur_dir)
import config

sys.path.append("%s/../resource" % cur_dir)
from lijiacai.utils import tool


class Cront():
    def __init__(self):
        self.try_time = 10

    def _id_func_vehicle(self, data):
        s = data.get("HEAD")
        m = hashlib.md5()
        b = s.encode(encoding='utf-8')
        m.update(b)
        str_md5 = m.hexdigest()
        return str_md5

    def _id_func_order(self, data):
        s = data.get("HEAD") + str(data.get("ACTION_TIME")) + str(data.get("NO")) + data.get("NO_TYPE")
        s = str(s)
        m = hashlib.md5()
        b = s.encode(encoding='utf-8')
        m.update(b)
        str_md5 = m.hexdigest()
        return str_md5

    def getdate(self, beforeOfDay):
        today = datetime.datetime.now()
        offset = datetime.timedelta(days=-beforeOfDay)
        re_date = (today + offset).strftime('%Y-%m-%d')
        return re_date

    def time_range(self):
        # res = (self.getdate(14),self.getdate(7))
        return ("2019-11-01", "2020-01-01")

    def update_vehicle_base_info(self):
        conf = config.vehicle_base_info
        sql = conf.get("sql")
        sql_fields = conf.get("fields")
        index = conf.get("index")
        es_fields = conf.get("es_fields", {})
        apply_func = None
        _id_func = self._id_func_vehicle

        ####### todo
        # sql = sql.format(*self.time_range())
        ######
        for i in range(self.try_time):
            try:
                O2O = tool.Oracle2Es()
                O2O.update(sql=sql, sql_fields=sql_fields, index=index, es_fields=es_fields,
                           _id_func=_id_func)
                O2O.close()
                break
            except Exception as e:
                logging.exception(str(e))
                O2O = None
                try:
                    O2O.close()
                except:
                    pass

    def update_order_flow(self):
        conf = config.order_flow
        sql = conf.get("sql")
        sql_fields = conf.get("fields")
        index = conf.get("index")
        es_fields = conf.get("es_fields", {})
        apply_func = None
        _id_func = self._id_func_order

        ####### todo
        sql = sql.format(*self.time_range())
        ######

        for i in range(self.try_time):
            try:
                O2O = tool.Oracle2Es()
                O2O.update(sql=sql, sql_fields=sql_fields, index=index, es_fields=es_fields,
                           _id_func=_id_func)
                O2O.close()
                break
            except Exception as e:
                logging.exception(str(e))
                O2O = None
                try:
                    O2O.close()
                except:
                    pass

    def update_order_cancle(self):
        conf = config.order_cancle
        sql = conf.get("sql")
        sql_fields = conf.get("fields")
        index = conf.get("index")
        es_fields = conf.get("es_fields", {})
        apply_func = None
        _id_func = self._id_func_order

        ####### todo
        sql = sql.format(*self.time_range())
        ######

        for i in range(self.try_time):
            try:
                O2O = tool.Oracle2Es()
                O2O.update(sql=sql, sql_fields=sql_fields, index=index, es_fields=es_fields,
                           _id_func=_id_func)
                O2O.close()
                break
            except Exception as e:
                logging.exception(str(e))
                O2O = None
                try:
                    O2O.close()
                except:
                    pass

    def update_user_order_flow(self):
        conf = config.user_order_flow
        sql = conf.get("sql")
        sql_fields = conf.get("fields")
        index = conf.get("index")
        es_fields = conf.get("es_fields", {})
        apply_func = None
        _id_func = self._id_func_order

        ####### todo
        sql = sql.format(*self.time_range())
        ######

        for i in range(self.try_time):
            try:
                O2O = tool.Oracle2Es()
                O2O.update(sql=sql, sql_fields=sql_fields, index=index, es_fields=es_fields,
                           _id_func=_id_func)
                O2O.close()
                break
            except Exception as e:
                logging.exception(str(e))
                O2O = None
                try:
                    O2O.close()
                except:
                    pass

    def update_user_order_cancle(self):
        conf = config.user_order_cancle
        sql = conf.get("sql")
        sql_fields = conf.get("fields")
        index = conf.get("index")
        es_fields = conf.get("es_fields", {})
        apply_func = None
        _id_func = self._id_func_order

        ####### todo
        sql = sql.format(*self.time_range())
        ######

        for i in range(self.try_time):
            try:
                O2O = tool.Oracle2Es()
                O2O.update(sql=sql, sql_fields=sql_fields, index=index, es_fields=es_fields,
                           _id_func=_id_func)
                O2O.close()
                break
            except Exception as e:
                logging.exception(str(e))
                O2O = None
                try:
                    O2O.close()
                except:
                    pass

    def update_station_order_flow(self):
        conf = config.station_order_flow
        sql = conf.get("sql")
        sql_fields = conf.get("fields")
        index = conf.get("index")
        es_fields = conf.get("es_fields", {})
        apply_func = None
        _id_func = self._id_func_order

        ####### todo
        sql = sql.format(*self.time_range())
        ######

        for i in range(self.try_time):
            try:
                O2O = tool.Oracle2Es()
                O2O.update(sql=sql, sql_fields=sql_fields, index=index, es_fields=es_fields,
                           _id_func=_id_func)
                O2O.close()
                break
            except Exception as e:
                logging.exception(str(e))
                O2O = None
                try:
                    O2O.close()
                except:
                    pass

    def update_station_order_cancle(self):
        conf = config.station_order_cancle
        sql = conf.get("sql")
        sql_fields = conf.get("fields")
        index = conf.get("index")
        es_fields = conf.get("es_fields", {})
        apply_func = None
        _id_func = self._id_func_order

        ####### todo
        sql = sql.format(*self.time_range())
        ######

        for i in range(self.try_time):
            try:
                O2O = tool.Oracle2Es()
                O2O.update(sql=sql, sql_fields=sql_fields, index=index, es_fields=es_fields,
                           _id_func=_id_func)
                O2O.close()
                break
            except Exception as e:
                logging.exception(str(e))
                O2O = None
                try:
                    O2O.close()
                except:
                    pass

    def update_license_dispatcher(self):
        conf = config.license_dispatcher
        sql = conf.get("sql")
        sql_fields = conf.get("fields")
        index = conf.get("index")
        es_fields = conf.get("es_fields", {})
        apply_func = None
        _id_func = self._id_func_order

        ####### todo
        sql = sql.format("2019-12-01", "2020-01-01")
        ######

        for i in range(self.try_time):
            try:
                O2O = tool.Mysql2Es()
                O2O.update_worksheet(sql=sql, sql_fields=sql_fields, index=index, es_fields=es_fields,
                                     _id_func=_id_func)
                O2O.close()
                break
            except Exception as e:
                logging.exception(str(e))
                O2O = None
                try:
                    O2O.close()
                except:
                    pass

    def update_user_dispatcher(self):
        conf = config.user_dispatcher
        sql = conf.get("sql")
        sql_fields = conf.get("fields")
        index = conf.get("index")
        es_fields = conf.get("es_fields", {})
        apply_func = None
        _id_func = self._id_func_order

        ####### todo
        sql = sql.format("2019-12-01", "2020-01-01")
        ######

        for i in range(self.try_time):
            try:
                O2O = tool.Mysql2Es()
                O2O.update_worksheet(sql=sql, sql_fields=sql_fields, index=index, es_fields=es_fields,
                                     _id_func=_id_func)
                O2O.close()
                break
            except Exception as e:
                logging.exception(str(e))
                O2O = None
                try:
                    O2O.close()
                except:
                    pass

    def update_station_dispatcher(self):
        conf = config.station_dispatcher
        sql = conf.get("sql")
        sql_fields = conf.get("fields")
        index = conf.get("index")
        es_fields = conf.get("es_fields", {})
        apply_func = None
        _id_func = self._id_func_order

        ####### todo
        sql = sql.format("2019-12-01", "2020-01-01")
        ######

        for i in range(self.try_time):
            try:
                M2E = tool.Mysql2Es()
                M2E.update_worksheet(sql=sql, sql_fields=sql_fields, index=index, es_fields=es_fields,
                                     _id_func=_id_func)
                M2E.close()
                break
            except Exception as e:
                logging.exception(str(e))
                O2O = None
                try:
                    O2O.close()
                except:
                    pass

    def update_license_log(self):
        conf = config.license_log
        sql = conf.get("sql")
        sql_fields = conf.get("fields")
        index = conf.get("index")
        es_fields = conf.get("es_fields", {})
        apply_func = None
        _id_func = self._id_func_order

        ####### todo
        sql = sql.format(*self.time_range())
        ######

        for i in range(self.try_time):
            try:
                O2O = tool.Oracle2Es()
                O2O.update(sql=sql, sql_fields=sql_fields, index=index, es_fields=es_fields,
                           _id_func=_id_func)
                O2O.close()
                break
            except Exception as e:
                logging.exception(str(e))
                O2O = None
                try:
                    O2O.close()
                except:
                    pass

    def update_user_log(self):
        conf = config.user_log
        sql = conf.get("sql")
        sql_fields = conf.get("fields")
        index = conf.get("index")
        es_fields = conf.get("es_fields", {})
        apply_func = None
        _id_func = self._id_func_order

        ####### todo
        sql = sql.format(*self.time_range())
        ######

        for i in range(self.try_time):
            try:
                O2O = tool.Oracle2Es()
                O2O.update(sql=sql, sql_fields=sql_fields, index=index, es_fields=es_fields,
                           _id_func=_id_func)
                O2O.close()
                break
            except Exception as e:
                logging.exception(str(e))
                O2O = None
                try:
                    O2O.close()
                except:
                    pass

    def update_user_relieve_log(self):
        conf = config.user_relieve_log
        sql = conf.get("sql")
        sql_fields = conf.get("fields")
        index = conf.get("index")
        es_fields = conf.get("es_fields", {})
        apply_func = None
        _id_func = self._id_func_order

        ####### todo
        sql = sql.format(*self.time_range())
        ######

        for i in range(self.try_time):
            try:
                O2O = tool.Oracle2Es()
                O2O.update(sql=sql, sql_fields=sql_fields, index=index, es_fields=es_fields,
                           _id_func=_id_func)
                O2O.close()
                break
            except Exception as e:
                logging.exception(str(e))
                O2O = None
                try:
                    O2O.close()
                except:
                    pass


def run():
    crontab = Cront()
    #######################
    update_vehicle_base_info = crontab.update_vehicle_base_info
    schedule.every().day.at("01:00").do(update_vehicle_base_info)
    #######################
    update_order_flow = crontab.update_order_flow
    schedule.every().day.at("02:00").do(update_order_flow)
    #######################
    update_order_cancle = crontab.update_order_cancle
    schedule.every().day.at("03:00").do(update_order_cancle)
    #######################
    update_user_order_flow = crontab.update_user_order_flow
    schedule.every().day.at("04:00").do(update_user_order_flow)
    #######################
    update_user_order_cancle = crontab.update_user_order_cancle
    schedule.every().day.at("05:00").do(update_user_order_cancle)
    #######################
    update_station_order_flow = crontab.update_station_order_flow
    schedule.every().day.at("06:00").do(update_station_order_flow)
    #######################
    update_station_order_cancle = crontab.update_station_order_cancle
    schedule.every().day.at("07:00").do(update_station_order_cancle)
    #######################
    update_station_order_cancle = crontab.update_license_dispatcher
    schedule.every().day.at("08:00").do(update_station_order_cancle)
    #######################
    update_user_dispatcher = crontab.update_user_dispatcher
    schedule.every().day.at("01:00").do(update_user_dispatcher)
    #######################
    update_station_dispatcher = crontab.update_station_dispatcher
    schedule.every().day.at("02:00").do(update_station_dispatcher)
    #######################
    update_license_log = crontab.update_license_log
    schedule.every().day.at("09:00").do(update_license_log)
    #######################
    update_user_log = crontab.update_user_log
    schedule.every().day.at("00:00").do(update_user_log)
    #######################
    update_user_relieve_log = crontab.update_user_relieve_log
    schedule.every().day.at("01:30").do(update_user_relieve_log)
    #######################
    while True:
        schedule.run_pending()
        time.sleep(1)


def test():
    crontab = Cront()
    #######################
    crontab.update_vehicle_base_info()
    crontab.update_order_flow()
    crontab.update_order_cancle()
    crontab.update_user_order_flow()
    crontab.update_user_order_cancle()
    crontab.update_station_order_flow()
    crontab.update_station_order_cancle()
    crontab.update_license_dispatcher()
    crontab.update_user_dispatcher()
    crontab.update_station_dispatcher()
    crontab.update_license_log()
    crontab.update_user_log()


if __name__ == '__main__':
    test()
